package com.modifiertech.lineage.handler;

import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.SkillLevelForgetAllEvent;
import com.modifiertech.lineage.event.SkillLevelUpEvent;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.common.MinecraftForge;

public class SkillLevelUpHandler {
    //饿汉
    public static SkillLevelUpHandler INSTANCE=new SkillLevelUpHandler();
    private SkillLevelUpHandler(){
    }

    public void skillLevelUp(EntityPlayer entityPlayer, String skillName, int upgradeSkillLevel, PlayerData data){
        Lineage.logger.info("技能升级:{},{}",entityPlayer.getName(),data.playerWR.get().getName());
        MinecraftForge.EVENT_BUS.post(new SkillLevelUpEvent.Pre(entityPlayer,skillName,upgradeSkillLevel));
        boolean skillLevelUp=false;
        //技能升级,获取升级状态
        skillLevelUp=
                data.optSkillLearn(skillName,upgradeSkillLevel);
        //升级成功
        if(skillLevelUp){
            data.subSkillUndistributed(upgradeSkillLevel,skillName);
            //数据保存/同步
            data.saveAndSync();
            MinecraftForge.EVENT_BUS.post(new SkillLevelUpEvent.Post(entityPlayer,skillName,upgradeSkillLevel));
            //升级失败
        }else{
            MinecraftForge.EVENT_BUS.post(new SkillLevelUpEvent.PostErr(entityPlayer,skillName,upgradeSkillLevel));
        }
    }

    public void skillLevelUpCmdUse(EntityPlayer entityPlayer, String skillName, int upgradeSkillLevel, PlayerData data){
        MinecraftForge.EVENT_BUS.post(new SkillLevelUpEvent.Pre(entityPlayer,skillName,upgradeSkillLevel));
        boolean skillLevelUp=false;
        //技能升级,获取升级状态
        skillLevelUp=
                data.optSkillLearn(skillName,upgradeSkillLevel);
        //升级成功
        if(skillLevelUp){
            //数据保存/同步
            data.saveAndSync();
            MinecraftForge.EVENT_BUS.post(new SkillLevelUpEvent.Post(entityPlayer,skillName,upgradeSkillLevel));
            //升级失败
        }else{
            MinecraftForge.EVENT_BUS.post(new SkillLevelUpEvent.PostErr(entityPlayer,skillName,upgradeSkillLevel));
        }
    }

    public void skillForgetAllCmdUse(EntityPlayer entityPlayer, PlayerData data){
        MinecraftForge.EVENT_BUS.post(new SkillLevelForgetAllEvent.Pre(entityPlayer));
        boolean skillForget=false;
        //技能升级,获取升级状态
        skillForget=
                data.optSkillForgetAll();
        //升级成功
        if(skillForget){
            //数据保存/同步
            data.saveAndSync();
            MinecraftForge.EVENT_BUS.post(new SkillLevelForgetAllEvent.Post(entityPlayer));
            //升级失败
        }else{
            MinecraftForge.EVENT_BUS.post(new SkillLevelForgetAllEvent.PostErr(entityPlayer));
        }
    }


}
